package com.smartjobs.reasoner;

import java.util.ArrayList;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Bag;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Seq;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.PrintUtil;
import com.smartjobs.bean.JobApplicationsVO;
import com.smartjobs.bean.JobPostingVO;
import com.smartjobs.bean.JobSeekerVO;
import com.smartjobs.util.RuntimeSettings;


public class ReasonerQuery {
	
	public static void main(String[] args) {
		
		
		   ArrayList<JobPostingVO> allJobs= getAllJobs();
		   
		   for(JobPostingVO jv:allJobs)
		   {
			   System.out.println(jv.getCompanyName());
			   System.out.println(jv.getJobCode());
			   System.out.println(jv.getJobId());
			   System.out.println(jv.getJobDesc());
			   for(String skills:jv.getSkills())
			   {
				   System.out.println(skills);
			   }
			   System.out.println();
		   }
		   
		   
		   ArrayList<JobApplicationsVO> applications = getAllApplications();
		   
		   for(JobApplicationsVO ja:applications)
		   {
			   System.out.println(ja.getAppId());
			   System.out.println(ja.getJobposting().getCompanyName());
			   System.out.println(ja.getJobSeeker().getName());
			   
		   }
		    
		   
	}
	
	public static void printStatements(Model m, Resource s, Property p, Resource o) {
        for (StmtIterator i = m.listStatements(s,p,o); i.hasNext(); ) {
            Statement stmt = i.nextStatement();
            System.out.println(" - " + PrintUtil.print(stmt));
        }
    }
	
	
	public static ArrayList<JobPostingVO> getAllJobs()
	{
		
         Model data = FileManager.get().loadModel("file:"+RuntimeSettings.location+"/MergedOntology.owl");
		 ArrayList<JobPostingVO> allJobs = new ArrayList<JobPostingVO>();
		 
		 OntModel  jobPosting = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, data);
		 OntClass smartJobs = jobPosting.getOntClass( "http://www.smartJobs.com/vocab/jobposting_skills" );
		 OntClass jobs = smartJobs.getEquivalentClass();
		 Iterator it1=null,it=null;
		 
		 try
		 {
		  it1 = jobs.listInstances();
	      it=smartJobs.listInstances();
	      ArrayList jobIdList = new ArrayList();
	      JobPostingVO jobpostVo=null;
	      ArrayList skills=null;
		 while(it.hasNext())
		 {
			 Resource r = (Resource)it.next();
			 ObjectProperty op=jobPosting.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_skills_jobId");
			 Resource r2=r.getProperty(op).getObject().asResource();
			
			 DatatypeProperty dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobId");
			 String jobId= r2.getProperty(dp).getString();
			 if(!jobIdList.contains(jobId))
			 {
				 
		     jobIdList.add(jobId);
		     jobpostVo = new JobPostingVO(); 	
		     skills = new ArrayList();
			 jobpostVo.setJobId(r2.getProperty(dp).getInt());
			 
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobCode");
			 jobpostVo.setJobCode(r2.getProperty(dp).getString());
			
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_location");
			 jobpostVo.setLocation(r2.getProperty(dp).getString());
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobDesc");
			 jobpostVo.setJobDesc(r2.getProperty(dp).getString());
			 
			 
			 op = jobPosting.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_companyId");
			 Resource r1=r2.getProperty(op).getObject().asResource();
			 dp=jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyName");
			 jobpostVo.setCompanyName(r1.getProperty(dp).getString());
			 dp=jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyId");
			 jobpostVo.setCompanyId(r1.getProperty(dp).getString());
			 dp=jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_emailId");
			 jobpostVo.setCompanyEmailId(r1.getProperty(dp).getString());
			 jobpostVo.setSource("Parent");
			
			 }
			 
			 
			 
			 op=jobPosting.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_skills_skillId");
			 Resource r3=r.getProperty(op).getObject().asResource();
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/skills_skillName");
			 skills.add(r3.getProperty(dp).getString());
			 jobpostVo.setSkills(skills);
			 allJobs.add(jobpostVo);
		 }
		 }
		 catch(Exception ex)
		 {
			 ex.printStackTrace();
		 }
		 
		 
		 try
		 {
			 JobPostingVO jobpostVo=null;
			 ArrayList jobIdList = new ArrayList();
			 ArrayList skills=null;
		 while(it1.hasNext())
		 {
			 Resource r = (Resource)it1.next();
			 ObjectProperty op=jobPosting.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_skills_jobId");
			 Resource r2=r.getProperty(op.getEquivalentProperty()).getObject().asResource();
			 DatatypeProperty dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobId");
			 String jobId=r2.getProperty(dp.getEquivalentProperty()).getString();
			 if(!jobIdList.contains(jobId))
			 {
				 
		     jobIdList.add(jobId);
			 jobpostVo = new JobPostingVO();
			 skills = new ArrayList();
			 jobpostVo.setJobId(r2.getProperty(dp.getEquivalentProperty()).getInt());
			  dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobCode");
			 jobpostVo.setJobCode(r2.getProperty(dp.getEquivalentProperty()).getString());
			 
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_location");
			 jobpostVo.setLocation(r2.getProperty(dp.getEquivalentProperty()).getString());
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobDesc");
			 jobpostVo.setJobDesc(r2.getProperty(dp.getEquivalentProperty()).getString());
			 
			 
			 op = jobPosting.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_companyId");
			 Resource r1=r2.getProperty(op.getEquivalentProperty()).getObject().asResource();
			 dp=jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyName");
			 jobpostVo.setCompanyName(r1.getProperty(dp.getEquivalentProperty()).getString());
			 dp=jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyId");
			 jobpostVo.setCompanyId(r1.getProperty(dp.getEquivalentProperty()).getString());
			 dp=jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_emailId");
			 jobpostVo.setCompanyEmailId(r1.getProperty(dp.getEquivalentProperty()).getString());
			 jobpostVo.setSource("Other");
			 }
			 
			 op=jobPosting.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_skills_skillId");
			 Resource r3=r.getProperty(op.getEquivalentProperty()).getObject().asResource();
			 dp = jobPosting.getDatatypeProperty("http://www.smartJobs.com/vocab/skills_skillName");
			 skills.add(r3.getProperty(dp.getEquivalentProperty()).getString());
			 jobpostVo.setSkills(skills);
			 allJobs.add(jobpostVo);
			
			 
			 
		 }
		 
		 }
		 catch(Exception ex)
		 {
			 
			 ex.printStackTrace();
		 }
		    
		
		return allJobs;
	}
	
	
	public static ArrayList<JobApplicationsVO> getAllApplications()
	{
		
         Model data = FileManager.get().loadModel("file:"+RuntimeSettings.location+"/MergedOntology.owl");
		 ArrayList<JobApplicationsVO> allApplications = new ArrayList<JobApplicationsVO>();
		 
		 OntModel  smartJobs = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, data);
		 Iterator classes=smartJobs.listClasses();
		 
		 
		 OntClass application = smartJobs.getOntClass( "http://www.smartJobs.com/vocab/application" );
		 OntClass application1 = application.getEquivalentClass();
		
	     Iterator it=application.listInstances();
	     
		 while(it.hasNext())
		 {
			 Resource r = (Resource)it.next();
			 JobApplicationsVO jobAppl = new JobApplicationsVO();
			 DatatypeProperty dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/application_appId");
			 jobAppl.setAppId(r.getProperty(dp).getInt());
			 ObjectProperty op = smartJobs.getObjectProperty("http://www.smartJobs.com/vocab/application_jobId");
			 Resource r1=r.getProperty(op).getObject().asResource();
			 JobPostingVO jobpostVo = new JobPostingVO();
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobCode");
			 jobpostVo.setJobCode(r1.getProperty(dp).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobId");
			 jobpostVo.setJobId(r1.getProperty(dp).getInt());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_location");
			 jobpostVo.setLocation(r1.getProperty(dp).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobDesc");
			 jobpostVo.setJobDesc(r1.getProperty(dp).getString());
			 jobAppl.setJobposting(jobpostVo);
			 
			  op = smartJobs.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_companyId");
			 Resource r2=r1.getProperty(op).getObject().asResource();
			 dp=smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyName");
			 jobpostVo.setCompanyName(r2.getProperty(dp).getString());
			 dp=smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyId");
			 jobpostVo.setCompanyId(r2.getProperty(dp).getString());
			 dp=smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_emailId");
			 jobpostVo.setCompanyEmailId(r2.getProperty(dp).getString());
			 
			 
			 op = smartJobs.getObjectProperty("http://www.smartJobs.com/vocab/application_jobSeekerId");
			 r1=r.getProperty(op).getObject().asResource();
			 JobSeekerVO jobSeeker = new JobSeekerVO();
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_name");
			 jobSeeker.setName(r1.getProperty(dp).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_emailId");
			 jobSeeker.setEmailId(r1.getProperty(dp).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_dob");
			
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_jobSeekerId");
			 jobSeeker.setId(r1.getProperty(dp).getInt());
			
			 jobAppl.setJobSeeker(jobSeeker);
			 
			 
			 
			 allApplications.add(jobAppl);
			 
			 
		 }
		 
		 Iterator it1 = application1.listInstances();
		 
		 while(it1.hasNext())
		 {
			 Resource r = (Resource)it1.next();
			 JobApplicationsVO jobAppl = new JobApplicationsVO();
			 DatatypeProperty dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/application_appId");
			 jobAppl.setAppId(r.getProperty(dp.getEquivalentProperty()).getInt());
			 ObjectProperty op = smartJobs.getObjectProperty("http://www.smartJobs.com/vocab/application_jobId");
			 Resource r1=r.getProperty(op.getEquivalentProperty()).getObject().asResource();
			 JobPostingVO jobpostVo = new JobPostingVO();
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobCode");
			 jobpostVo.setJobCode(r1.getProperty(dp.getEquivalentProperty()).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobId");
			 jobpostVo.setJobId(r1.getProperty(dp.getEquivalentProperty()).getInt());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_location");
			 jobpostVo.setLocation(r1.getProperty(dp.getEquivalentProperty()).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobposting_jobDesc");
			 jobpostVo.setJobDesc(r1.getProperty(dp.getEquivalentProperty()).getString());
			 jobAppl.setJobposting(jobpostVo);
			 
			 op = smartJobs.getObjectProperty("http://www.smartJobs.com/vocab/jobposting_companyId");
			 Resource r2=r1.getProperty(op.getEquivalentProperty()).getObject().asResource();
			 dp=smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyName");
			 jobpostVo.setCompanyName(r2.getProperty(dp.getEquivalentProperty()).getString());
			 dp=smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_companyId");
			 jobpostVo.setCompanyId(r2.getProperty(dp.getEquivalentProperty()).getString());
			 dp=smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/companyprofile_emailId");
			 jobpostVo.setCompanyEmailId(r2.getProperty(dp.getEquivalentProperty()).getString());
			 
			 
			 op = smartJobs.getObjectProperty("http://www.smartJobs.com/vocab/application_jobSeekerId");
			 r1=r.getProperty(op.getEquivalentProperty()).getObject().asResource();
			 JobSeekerVO jobSeeker = new JobSeekerVO();
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_name");
			 jobSeeker.setName(r1.getProperty(dp.getEquivalentProperty()).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_emailId");
			 jobSeeker.setEmailId(r1.getProperty(dp.getEquivalentProperty()).getString());
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_dob");
			
			 dp = smartJobs.getDatatypeProperty("http://www.smartJobs.com/vocab/jobseeker_jobSeekerId");
			 jobSeeker.setId(r1.getProperty(dp.getEquivalentProperty()).getInt());
			
			 jobAppl.setJobSeeker(jobSeeker);
			 
			 
			 
			 allApplications.add(jobAppl);
			 
			 
		 }
		 
		
		    
		
		return allApplications;
	}
	
	
	
	
	

}
